Conditions | 51 |
Total Lines | 261 |
Code Lines | 110 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like date.js ➔ date often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | function date (format, timestamp) { |
||
2 | // http://kevin.vanzonneveld.net |
||
3 | // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com) |
||
4 | // + parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html) |
||
5 | // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
||
6 | // + improved by: MeEtc (http://yass.meetcweb.com) |
||
7 | // + improved by: Brad Touesnard |
||
8 | // + improved by: Tim Wiel |
||
9 | // + improved by: Bryan Elliott |
||
10 | // |
||
11 | // + improved by: Brett Zamir (http://brett-zamir.me) |
||
12 | // + improved by: David Randall |
||
13 | // + input by: Brett Zamir (http://brett-zamir.me) |
||
14 | // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
||
15 | // + improved by: Brett Zamir (http://brett-zamir.me) |
||
16 | // + improved by: Brett Zamir (http://brett-zamir.me) |
||
17 | // + improved by: Theriault |
||
18 | // + derived from: gettimeofday |
||
19 | // + input by: majak |
||
20 | // + bugfixed by: majak |
||
21 | // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
||
22 | // + input by: Alex |
||
23 | // + bugfixed by: Brett Zamir (http://brett-zamir.me) |
||
24 | // + improved by: Theriault |
||
25 | // + improved by: Brett Zamir (http://brett-zamir.me) |
||
26 | // + improved by: Theriault |
||
27 | // + improved by: Thomas Beaucourt (http://www.webapp.fr) |
||
28 | // + improved by: JT |
||
29 | // + improved by: Theriault |
||
30 | // + improved by: Rafał Kukawski (http://blog.kukawski.pl) |
||
31 | // + input by: Martin |
||
32 | // + input by: Alex Wilson |
||
33 | // % note 1: Uses global: php_js to store the default timezone |
||
34 | // % note 2: Although the function potentially allows timezone info (see notes), it currently does not set |
||
35 | // % note 2: per a timezone specified by date_default_timezone_set(). Implementers might use |
||
36 | // % note 2: this.php_js.currentTimezoneOffset and this.php_js.currentTimezoneDST set by that function |
||
37 | // % note 2: in order to adjust the dates in this function (or our other date functions!) accordingly |
||
38 | // * example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400); |
||
39 | // * returns 1: '09:09:40 m is month' |
||
40 | // * example 2: date('F j, Y, g:i a', 1062462400); |
||
41 | // * returns 2: 'September 2, 2003, 2:26 am' |
||
42 | // * example 3: date('Y W o', 1062462400); |
||
43 | // * returns 3: '2003 36 2003' |
||
44 | // * example 4: x = date('Y m d', (new Date()).getTime()/1000); |
||
45 | // * example 4: (x+'').length == 10 // 2009 01 09 |
||
46 | // * returns 4: true |
||
47 | // * example 5: date('W', 1104534000); |
||
48 | // * returns 5: '53' |
||
49 | // * example 6: date('B t', 1104534000); |
||
50 | // * returns 6: '999 31' |
||
51 | // * example 7: date('W U', 1293750000.82); // 2010-12-31 |
||
52 | // * returns 7: '52 1293750000' |
||
53 | // * example 8: date('W', 1293836400); // 2011-01-01 |
||
54 | // * returns 8: '52' |
||
55 | // * example 9: date('W Y-m-d', 1293974054); // 2011-01-02 |
||
56 | // * returns 9: '52 2011-01-02' |
||
57 | var that = this, |
||
58 | jsdate, f, formatChr = /\\?([a-z])/gi, |
||
59 | formatChrCb, |
||
60 | // Keep this here (works, but for code commented-out |
||
61 | // below for file size reasons) |
||
62 | //, tal= [], |
||
63 | _pad = function (n, c) { |
||
64 | if ((n = n + '').length < c) { |
||
65 | return new Array((++c) - n.length).join('0') + n; |
||
66 | } |
||
67 | return n; |
||
68 | }, |
||
69 | txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; |
||
70 | formatChrCb = function (t, s) { |
||
71 | return f[t] ? f[t]() : s; |
||
72 | }; |
||
73 | f = { |
||
74 | // Day |
||
75 | d: function () { // Day of month w/leading 0; 01..31 |
||
76 | return _pad(f.j(), 2); |
||
77 | }, |
||
78 | D: function () { // Shorthand day name; Mon...Sun |
||
79 | return f.l().slice(0, 3); |
||
80 | }, |
||
81 | j: function () { // Day of month; 1..31 |
||
82 | return jsdate.getDate(); |
||
83 | }, |
||
84 | l: function () { // Full day name; Monday...Sunday |
||
85 | return egw && egw.lang ? egw.lang(txt_words[f.w()] + 'day') : txt_words[f.w()] + 'day'; |
||
86 | }, |
||
87 | N: function () { // ISO-8601 day of week; 1[Mon]..7[Sun] |
||
88 | return f.w() || 7; |
||
89 | }, |
||
90 | S: function () { // Ordinal suffix for day of month; st, nd, rd, th |
||
91 | var j = f.j(); |
||
92 | return j > 4 || j < 21 ? 'th' : {1: 'st', 2: 'nd', 3: 'rd'}[j % 10] || 'th'; |
||
93 | }, |
||
94 | w: function () { // Day of week; 0[Sun]..6[Sat] |
||
95 | return jsdate.getDay(); |
||
96 | }, |
||
97 | z: function () { // Day of year; 0..365 |
||
98 | var a = new Date(f.Y(), f.n() - 1, f.j()), |
||
99 | b = new Date(f.Y(), 0, 1); |
||
100 | return Math.round((a - b) / 864e5) + 1; |
||
101 | }, |
||
102 | |||
103 | // Week |
||
104 | W: function () { // ISO-8601 week number |
||
105 | var a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3), |
||
106 | b = new Date(a.getFullYear(), 0, 4); |
||
107 | return _pad(1 + Math.round((a - b) / 864e5 / 7), 2); |
||
108 | }, |
||
109 | |||
110 | // Month |
||
111 | F: function () { // Full month name; January...December |
||
112 | return egw && egw.lang ? egw.lang(txt_words[6 + f.n()]) : txt_words[6 + f.n()]; |
||
113 | }, |
||
114 | m: function () { // Month w/leading 0; 01...12 |
||
115 | return _pad(f.n(), 2); |
||
116 | }, |
||
117 | M: function () { // Shorthand month name; Jan...Dec |
||
118 | return f.F().slice(0, 3); |
||
119 | }, |
||
120 | n: function () { // Month; 1...12 |
||
121 | return jsdate.getMonth() + 1; |
||
122 | }, |
||
123 | t: function () { // Days in month; 28...31 |
||
124 | return (new Date(f.Y(), f.n(), 0)).getDate(); |
||
125 | }, |
||
126 | |||
127 | // Year |
||
128 | L: function () { // Is leap year?; 0 or 1 |
||
129 | return new Date(f.Y(), 1, 29).getMonth() === 1 | 0; |
||
130 | }, |
||
131 | o: function () { // ISO-8601 year |
||
132 | var n = f.n(), |
||
133 | W = f.W(), |
||
134 | Y = f.Y(); |
||
135 | return Y + (n === 12 && W < 9 ? -1 : n === 1 && W > 9); |
||
136 | }, |
||
137 | Y: function () { // Full year; e.g. 1980...2010 |
||
138 | return jsdate.getFullYear(); |
||
139 | }, |
||
140 | y: function () { // Last two digits of year; 00...99 |
||
141 | return (f.Y() + "").slice(-2); |
||
142 | }, |
||
143 | |||
144 | // Time |
||
145 | a: function () { // am or pm |
||
146 | return jsdate.getHours() > 11 ? "pm" : "am"; |
||
147 | }, |
||
148 | A: function () { // AM or PM |
||
149 | return f.a().toUpperCase(); |
||
150 | }, |
||
151 | B: function () { // Swatch Internet time; 000..999 |
||
152 | var H = jsdate.getUTCHours() * 36e2, |
||
153 | // Hours |
||
154 | i = jsdate.getUTCMinutes() * 60, |
||
155 | // Minutes |
||
156 | s = jsdate.getUTCSeconds(); // Seconds |
||
157 | return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3); |
||
158 | }, |
||
159 | g: function () { // 12-Hours; 1..12 |
||
160 | return f.G() % 12 || 12; |
||
161 | }, |
||
162 | G: function () { // 24-Hours; 0..23 |
||
163 | return jsdate.getHours(); |
||
164 | }, |
||
165 | h: function () { // 12-Hours w/leading 0; 01..12 |
||
166 | return _pad(f.g(), 2); |
||
167 | }, |
||
168 | H: function () { // 24-Hours w/leading 0; 00..23 |
||
169 | return _pad(f.G(), 2); |
||
170 | }, |
||
171 | i: function () { // Minutes w/leading 0; 00..59 |
||
172 | return _pad(jsdate.getMinutes(), 2); |
||
173 | }, |
||
174 | s: function () { // Seconds w/leading 0; 00..59 |
||
175 | return _pad(jsdate.getSeconds(), 2); |
||
176 | }, |
||
177 | u: function () { // Microseconds; 000000-999000 |
||
178 | return _pad(jsdate.getMilliseconds() * 1000, 6); |
||
179 | }, |
||
180 | |||
181 | // Timezone |
||
182 | e: function () { // Timezone identifier; e.g. Atlantic/Azores, ... |
||
183 | // The following works, but requires inclusion of the very large |
||
184 | // timezone_abbreviations_list() function. |
||
185 | /* return this.date_default_timezone_get(); |
||
186 | */ |
||
187 | throw 'Not supported (see source code of date() for timezone on how to add support)'; |
||
188 | }, |
||
189 | I: function () { // DST observed?; 0 or 1 |
||
190 | // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC. |
||
191 | // If they are not equal, then DST is observed. |
||
192 | var a = new Date(f.Y(), 0), |
||
193 | // Jan 1 |
||
194 | c = Date.UTC(f.Y(), 0), |
||
195 | // Jan 1 UTC |
||
196 | b = new Date(f.Y(), 6), |
||
197 | // Jul 1 |
||
198 | d = Date.UTC(f.Y(), 6); // Jul 1 UTC |
||
199 | return 0 + ((a - c) !== (b - d)); |
||
200 | }, |
||
201 | O: function () { // Difference to GMT in hour format; e.g. +0200 |
||
202 | var a = jsdate.getTimezoneOffset(); |
||
203 | return (a > 0 ? "-" : "+") + _pad(Math.abs(a / 60 * 100), 4); |
||
204 | }, |
||
205 | P: function () { // Difference to GMT w/colon; e.g. +02:00 |
||
206 | var O = f.O(); |
||
207 | return (O.substr(0, 3) + ":" + O.substr(3, 2)); |
||
208 | }, |
||
209 | T: function () { // Timezone abbreviation; e.g. EST, MDT, ... |
||
210 | // The following works, but requires inclusion of the very |
||
211 | // large timezone_abbreviations_list() function. |
||
212 | /* var abbr = '', i = 0, os = 0, default = 0; |
||
213 | if (!tal.length) { |
||
214 | tal = that.timezone_abbreviations_list(); |
||
215 | } |
||
216 | if (that.php_js && that.php_js.default_timezone) { |
||
217 | default = that.php_js.default_timezone; |
||
218 | for (abbr in tal) { |
||
219 | for (i=0; i < tal[abbr].length; i++) { |
||
220 | if (tal[abbr][i].timezone_id === default) { |
||
221 | return abbr.toUpperCase(); |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | for (abbr in tal) { |
||
227 | for (i = 0; i < tal[abbr].length; i++) { |
||
228 | os = -jsdate.getTimezoneOffset() * 60; |
||
229 | if (tal[abbr][i].offset === os) { |
||
230 | return abbr.toUpperCase(); |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | */ |
||
235 | return 'UTC'; |
||
236 | }, |
||
237 | Z: function () { // Timezone offset in seconds (-43200...50400) |
||
238 | return -jsdate.getTimezoneOffset() * 60; |
||
239 | }, |
||
240 | |||
241 | // Full Date/Time |
||
242 | c: function () { // ISO-8601 date. |
||
243 | return 'Y-m-d\\Th:i:sP'.replace(formatChr, formatChrCb); |
||
244 | }, |
||
245 | r: function () { // RFC 2822 |
||
246 | return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb); |
||
247 | }, |
||
248 | U: function () { // Seconds since UNIX epoch |
||
249 | return jsdate.getTime() / 1000 | 0; |
||
250 | } |
||
251 | }; |
||
252 | this.date = function (format, timestamp) { |
||
253 | that = this; |
||
254 | jsdate = ((typeof timestamp === 'undefined' ||isNaN(timestamp.valueOf())) ? new Date() : // Not provided |
||
255 | (timestamp instanceof Date) ? new Date(timestamp) : // JS Date() |
||
256 | new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int) |
||
257 | ); |
||
258 | return format.replace(formatChr, formatChrCb); |
||
259 | }; |
||
260 | return this.date(format, timestamp); |
||
261 | } |
||
262 |